package com.lzx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.api.CommonResult;
import com.lzx.dto.UploadDto;
import com.lzx.entity.Brand;
import com.lzx.entity.Pictrue;
import com.lzx.entity.Product;
import com.lzx.service.BrandService;
import com.lzx.service.PictrueService;
import com.lzx.service.ProductService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Pictrue)表控制层
 *
 * @author makejava
 * @since 2021-03-01 15:14:03
 */
@RestController
@RequestMapping("/pictrue")
public class PictrueController {

    /**
     * 服务对象
     */
    @Resource
    private PictrueService pictrueService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Pictrue selectOne(Integer id) {
        return this.pictrueService.queryById(id);
    }

    /**
     * 获取所有图片
     * @return
     */
    @RequestMapping("/getDetailsPicture")
    public CommonResult<List<Pictrue>> getDetailsPicture(@RequestParam Integer productId){
        List<Pictrue> imgs = pictrueService.getAllByTypeAndTypeId("product", productId);
        return CommonResult.success(imgs);
    }



    @PostMapping(value = "/upload")
    public CommonResult<List<String>> uploadPic(@RequestParam List<MultipartFile> files ,@RequestParam String type,@RequestParam Integer typeId){
        if(BeanUtil.isEmpty(files)){
            return CommonResult.failed("图片不能为空");
        }
        if(StringUtil.isNullOrEmpty(type)|| BeanUtil.isEmpty(typeId)){
            return CommonResult.failed("图片所属类型和所属id不能为空");
        }
        if(!pictrueService.isItemHave(type,typeId)){
            return CommonResult.failed("图片所属成员不存在");
        }
        /*if("product".equals(type)){
            Product item = productService.getById(typeId);
            if(BeanUtil.isEmpty(item)){
                return CommonResult.failed("图片所属成员不存在,请重新输入正确的成员ID");
            }
        }
        if("brand".equals(type)){
            Brand item = brandService.getById(typeId);
            if(BeanUtil.isEmpty(item)){
                return CommonResult.failed("图片所属成员不存在,请重新输入正确的成员ID");
            }
        }*/

            Pictrue pictrue = new Pictrue();
            Pictrue defaultImg = pictrueService.getDefaultImg(type, typeId);
            if(!BeanUtil.isNotEmpty(defaultImg)){
                pictrue.setIsdefault(true);
            }
            files.forEach(file->{
                String picUrl = pictrueService.uploadPic(file, type);
                pictrue.setTypeid(typeId);
                pictrue.setType(type);
                pictrue.setSrc(picUrl);
                pictrueService.save(pictrue);
                pictrue.setIsdefault(false);
            });
            return CommonResult.success(200,"上传成功！");

    }
}
